#include "btform.h"
#include "ui_btform.h"
#include<QDebug>


BtForm* BtForm::currentActivateBt=nullptr;

BtForm::BtForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BtForm)
{
    ui->setupUi(this);
    setAnimal();

}

BtForm::~BtForm()
{
    delete ui;
}

void BtForm::setIconAndText(const QString &icon, const QString &btText,int pageId)
{
    ui->bticon->setPixmap(QPixmap(icon));

    ui->btText->setText(btText);

    this->pageID=pageId;
}

int BtForm::getPageId() const
{
    return pageID;
}

void BtForm::clearBackground()
{
//background-color:#F5F5F5;
    ui->btstyle->setStyleSheet("#btstyle:hover{background-color:#90EE90;}");
}

void BtForm::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);


    if(currentActivateBt&&currentActivateBt!=this){
        currentActivateBt->stopAnimal();
        qDebug()<<currentActivateBt->pageID<<":stop";
        currentActivateBt->ui->btstyle->setStyleSheet("#btstyle:hover{background-color:#90EE90;}");
    }

    currentActivateBt=this;
    startAnimal();
    qDebug()<<pageID<<":start";


    ui->btstyle->setStyleSheet("#btstyle{background-color:#808080;}");
    emit btClick(pageID);
    qDebug()<<"发送信号"<<pageID;

}

void BtForm::enterEvent(QEvent *event)
{
    Q_UNUSED(event);
    startAnimal();

}

void BtForm::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    if(currentActivateBt!=this){
        stopAnimal();
    }
}

void BtForm::setAnimal()
{

    line1Animal=new QPropertyAnimation(ui->line1,"geometry",this);
    line1Animal->setDuration(1500);
    line1Animal->setKeyValueAt(0,QRect(0,19,2,0));
    line1Animal->setKeyValueAt(0.5,QRect(0,2,2,19));
    line1Animal->setKeyValueAt(1,QRect(0,19,2,0));
    line1Animal->setLoopCount(-1);




   line2Animal=new QPropertyAnimation(ui->line2,"geometry",this);
   line2Animal->setDuration(1800);
   line2Animal->setKeyValueAt(0,QRect(9,19,2,0));
   line2Animal->setKeyValueAt(0.5,QRect(9,2,2,19));
   line2Animal->setKeyValueAt(1,QRect(9,19,2,0));
   line2Animal->setLoopCount(-1);


    line3Animal=new QPropertyAnimation(ui->line3,"geometry",this);
    line3Animal->setDuration(1200);
    line3Animal->setKeyValueAt(0,QRect(18,19,2,0));
    line3Animal->setKeyValueAt(0.5,QRect(18,2,2,19));
    line3Animal->setKeyValueAt(1,QRect(18,19,2,0));
    line3Animal->setLoopCount(-1);




    line4Animal=new QPropertyAnimation(ui->line4,"geometry",this);
    line4Animal->setDuration(1900);
    line4Animal->setKeyValueAt(0,QRect(27,19,2,0));
    line4Animal->setKeyValueAt(0.5,QRect(27,2,2,19));
    line4Animal->setKeyValueAt(1,QRect(27,19,2,0));
    line4Animal->setLoopCount(-1);
}

void BtForm::stopAnimal()
{

    line1Animal->stop();
    line2Animal->stop();
    line3Animal->stop();
    line4Animal->stop();

    ui->line1->setGeometry(QRect(0, 19, 2, 0));
    ui->line2->setGeometry(QRect(9, 19, 2, 0));
    ui->line3->setGeometry(QRect(18, 19, 2, 0));
    ui->line4->setGeometry(QRect(27, 19, 2, 0));
}

void BtForm::startAnimal()
{
    line2Animal->start();
    line3Animal->start();
    line4Animal->start();
    line1Animal->start();
}
